package com.cya.lotterysystem.controller.handler;

import com.cya.lotterysystem.common.errcode.GlobalErrorCodeConstans;
import com.cya.lotterysystem.common.exception.ControllerException;
import com.cya.lotterysystem.common.exception.ServiceException;
import com.cya.lotterysystem.common.pojo.CommonResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author ：陈奕安（3048279304@qq.com）
 * @date ：Created in 2025/10/29 10:27
 * @description：统一异常返回
 * @modified By：
 * @version:
 */
@RestControllerAdvice//可以捕获全局抛的异常
public class GlobalExceptionHandler {

    private final static Logger logger= LoggerFactory.getLogger(GlobalExceptionHandler.class);

    @ExceptionHandler(value = ServiceException.class)
    public CommonResult<?> serviceException(ServiceException e){
        logger.error("ServiceException:",e);
        System.out.println(e.getMessage());

        return CommonResult.error(GlobalErrorCodeConstans.INTERNAL_SERVER_ERROR.getCode(),e.getMessage());


    }
    @ExceptionHandler(value = ControllerException.class)
    public CommonResult<?> controllerException(ControllerException e){
        logger.error("controllerException:",e);

        return CommonResult.error(GlobalErrorCodeConstans.INTERNAL_SERVER_ERROR.getCode(),e.getMessage());

    }
    @ExceptionHandler(value = Exception.class)
    public CommonResult<?> Exception(Exception e){
        logger.error("Exception:",e);

        return CommonResult.error(GlobalErrorCodeConstans.INTERNAL_SERVER_ERROR );

    }

}